using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Collections;
using System.Globalization;
using EnvDTE;
using VSLangProj;

namespace XnaDesignerGenerator
{
    public class AssemblyListConverter : TypeConverter
    {
        // Methods
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return ((destinationType == typeof(Assembly)) || base.CanConvertTo(context, destinationType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                AssemblyName name = new AssemblyName(value.ToString());
                DTE service = (DTE)context.GetService(typeof(DTE));
                foreach (Project project in service.Solution.Projects)
                {
                    foreach (Reference reference in ((VSProject)project.Object).References)
                    {
                        if (reference.Identity == name.Name)
                            return Assembly.ReflectionOnlyLoadFrom(reference.Path);
                    }
                }
                return null;
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value is Assembly)
            {
                return ((Assembly)value).FullName;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            IList values = new ArrayList();
            DTE service = (DTE)context.GetService(typeof(DTE));
            try
            {
                foreach (Project project in service.Solution.Projects)
                {
                    foreach (Reference reference in ((VSProject)project.Object).References)
                    {
                        Console.WriteLine(reference.Identity);
                        values.Add(Assembly.ReflectionOnlyLoadFrom(reference.Path));
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return new TypeConverter.StandardValuesCollection(values);
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}
